html转pdf错误wkhtmltopdf

本人环境配置

wkhtmltopdf: 0.12.3.2
Python: 3.5.2+

问题描述

options = {
    'encoding': "UTF-8",
    'custom-header': [
        ('Accept-Encoding', 'gzip')
    ]
}
pdfkit.from_file(['0.html', '1.html'], 'demo.pdf', options=options)

在将多个html文件转换成pdf的时候, 提示:

This version of wkhtmltopdf is build against an unpatched version of QT

发现下面错误有时候pdf已经转换出来 try下 延时下就可以了

except :
    print '-------------xxxxx-----------'
    time.sleep(2)

Traceback (most recent call last):
  File "/path/to/jekyll_spider.py", line 148, in <module>
    main()
  File "/path/to/jekyll_spider.py", line 144, in main
    save_pdf(html_paths, file_name)
  File "/path/to/jekyll_spider.py", line 118, in save_pdf
    pdfkit.from_file(html_path, pdf_path, options=options)
  File "/usr/local/lib/python3.5/dist-packages/pdfkit/api.py", line 49, in from_file
    return r.to_pdf(output_path)
  File "/usr/local/lib/python3.5/dist-packages/pdfkit/pdfkit.py", line 156, in to_pdf
    raise IOError('wkhtmltopdf reported an error:\n' + stderr)
OSError: wkhtmltopdf reported an error:
Error: This version of wkhtmltopdf is build against an unpatched version of QT, and does not support more then one input document.
Exit with code 1, due to unknown error.

解决方法

下载并使用 0.12.4 版本的 wkhtmltopdf 包 1

$ wget https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.4/wkhtmltox-0.12.4_linux-generic-amd64.tar.xz
$ tar xvJf wkhtmltox-0.12.4_linux-generic-amd64.tar.xz
$ sudo cp wkhtmltox/bin/wkhtmlto* /usr/bin/

如果使用PDFkit而没有安装wkhtmltopdf, 则会报如下错误:

Traceback (most recent call last):
  File "/path/to/jekyll_spider.py", line 148, in <module>
    main()
  File "/path/to/jekyll_spider.py", line 144, in main
    save_pdf(html_paths, file_name)
  File "/path/to/jekyll_spider.py", line 118, in save_pdf
    pdfkit.from_file(html_path, pdf_path, options=options)
  File "/usr/local/lib/python3.5/dist-packages/pdfkit/api.py", line 47, in from_file
    configuration=configuration, cover_first=cover_first)
  File "/usr/local/lib/python3.5/dist-packages/pdfkit/pdfkit.py", line 42, in __init__
    self.configuration = (Configuration() if configuration is None
  File "/usr/local/lib/python3.5/dist-packages/pdfkit/configuration.py", line 27, in __init__
    'https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf' % self.wkhtmltopdf)
OSError: No wkhtmltopdf executable found: "b''"
If this file exists please check that this process can read it. Otherwise please install wkhtmltopdf - https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf

Ubuntu可以直接命令行安装wkhtmltopdf:

$ sudo apt-get install wkhtmltopdf
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值